package bar.dark.lee;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public abstract class SuperTabActivity extends ActivityGroup
{
	public Context superContext = this;
	
	public static Typeface typeface;
	
	private View titleLayout;
	private TextView titleView;
	private TextView leftButton;
	private TextView rightButton;
	private ViewGroup bodyLayout;
	
	public ViewGroup superAdView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		super.setContentView(R.layout.super_activity);
		
		titleLayout = findViewById(R.id.titleLayout);
		titleView = (TextView) findViewById(R.id.superTitleView);
		bodyLayout = (ViewGroup) findViewById(R.id.bodyLayout);
		leftButton = (TextView) findViewById(R.id.superLeftButton);
		rightButton = (TextView) findViewById(R.id.superRightButton);
		
		superAdView = (ViewGroup) findViewById(R.id.superAdview);
		
		leftButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				onBackPressed();
			}
		});
		
		rightButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				tipButton();
			}
		});
	}
	
	@Override
	public void setContentView(int layoutResID)
	{
		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(layoutResID, null);
		this.setContentView(view);
	}
	
	@Override
	public void setContentView(View view) {
		this.bodyLayout.removeAllViews();
		
		if(view==null)
		{
			return;
		}
		
		ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
		
		if(layoutParams == null)
		{
			layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		}
		else
		{
			layoutParams.width = LayoutParams.FILL_PARENT;
			layoutParams.height = LayoutParams.FILL_PARENT;
		}
		
		view.setLayoutParams(layoutParams);
		
		this.bodyLayout.addView(view);
		
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int fontFlag = pref.getInt(BDConstents.FONT_SETTING, 1);
		
		if (fontFlag == 1)
		{
			if (typeface == null)
				typeface = Typeface.createFromAsset(getAssets(), BDConstents.FONT_FILE_NAME);

	        ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
	        setGlobalFont(root); 
		}
	}
	
	private void setGlobalFont(ViewGroup root) 
	{
	    for (int i = 0; i < root.getChildCount(); i++) 
	    {
	        View child = root.getChildAt(i);
	        
	        if (child instanceof TextView)
	        {
	        	((TextView)child).setTypeface(typeface);
	        }
	        else if (child instanceof EditText)
			{
	        	((EditText)child).setTypeface(typeface);
			}
	        else if (child instanceof Button)
			{
	        	((Button)child).setTypeface(typeface);
			}
	        else if (child instanceof ViewGroup)
	        {
	        	setGlobalFont((ViewGroup)child);
	        }
	    }
	}
	
	@Override
	public void setTitle(CharSequence title)
	{
		titleView.setText(title);
	}

	@Override
	public void setTitle(int titleId)
	{
		titleView.setBackgroundResource(titleId);
	}
	
	/**
	 * 오른쪽 팁 버튼을 안보이게 한다.
	 */
	public void setGoneTipButton()
	{
		rightButton.setVisibility(View.INVISIBLE);
	}
	
	/**
	 * 타이틀을 안보이게 한다.
	 */
	public void setGoneTitle()
	{
		titleLayout.setVisibility(View.GONE);
	}
	
	/**
	 * 도움말 버튼입니다.
	 */
	protected abstract void tipButton();
}
